package com.bobi.ecofavor.controller;

import com.bobi.ecofavor.common.enums.CacheKeyEnum;
import com.bobi.ecofavor.common.enums.OrderStatusEnum;
import com.bobi.ecofavor.common.utils.RedisUtil;
import com.bobi.ecofavor.model.domain.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.bobi.ecofavor.common.utils.JsonResponse;
import com.bobi.ecofavor.service.OrderService;
import com.bobi.ecofavor.model.domain.Order;

import javax.servlet.http.HttpServletRequest;


/**
 *
 *  前端控制器
 *
 *
 * @author wenyuan
 * @since 2023-10-21
 * @version v1.0
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {

    private final Logger logger = LoggerFactory.getLogger( OrderController.class );

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private OrderService orderService;


    /**
     * 根据订单状态获取用户订单列表
     */
    @RequestMapping("/getUserOrders")
    public JsonResponse getUserOrders(Integer pageSize, Integer page, Integer status) {
        User user = (User) redisUtil.get(CacheKeyEnum.USER.getKey());
        Integer userId = user.getId();
        JsonResponse userOrderListPage = orderService.getUserOrderListPage(userId, pageSize, page, status);
        return userOrderListPage;
    }

    /**
     * 修改订单的状态
     */
    @RequestMapping("/updateOrderStatus")
    public JsonResponse updateOrderStatus(Integer id, Integer status) {
        // 1. 如果是付款
        if (status == OrderStatusEnum.PAID.getCode())
            return orderService.payBill(id);
        // 2. 其他是取消订单和确认完成
        else
            return orderService.updateOrderStatus(id, status);

    }

    /**
     * 根据订单id 查询订单
     */
    @RequestMapping("/getUserOrder")
    public JsonResponse getUserOrderById(Integer orderId) {
        User user = (User) redisUtil.get(CacheKeyEnum.USER.getKey());
        Integer userId = user.getId();
        JsonResponse userOrderById = orderService.getUserOrderById(userId, orderId);
        return userOrderById;
    }


    /**
     * 点击 购买键 则加入订单
     */
    @RequestMapping("/insertOrder")
    private JsonResponse insertOrder( Integer goodsId, Integer goodsNum, HttpServletRequest request){
        User user = (User) redisUtil.get(CacheKeyEnum.USER.getKey());
        JsonResponse success = orderService.insertOrder(user, goodsId, goodsNum);
        return success;
    }


    /**
     * 选择使用优惠券
     */
    @RequestMapping("/useCouponInOrder")
    public JsonResponse useCouponInOrder(Integer couponId, Integer orderId) {
        return orderService.useCouponInOrder(couponId, orderId);
    }

    /**
     * 付款
     */
    @RequestMapping("/payBill")
    public JsonResponse payBill(Integer orderId) {
        return orderService.payBill(orderId);
    }

    /**
     * 再来一单
     */
    @RequestMapping("/oneMore")
    public JsonResponse oneMore(Integer id) {
        return orderService.oneMore(id);
    }


    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Order  order =  orderService.getById(id);
        return JsonResponse.success(order);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        orderService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public JsonResponse updateOrder(Order  order) throws Exception {
        orderService.updateById(order);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Order
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public JsonResponse create(Order  order) throws Exception {
        orderService.save(order);
        return JsonResponse.success(null);
    }
}

